package org.cmoine.efavorites;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.cmoine.efavorites"; //$NON-NLS-1$

	public static final String ENABLED_IMAGE_ID="icons/enabled-star.png"; //$NON-NLS-1$
	public static final String DISABLED_IMAGE_ID="icons/disabled-star.png"; //$NON-NLS-1$
    public static final String PREFS_IMAGE_ID="icons/preferences.png"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	public static void log(int severity, String message, Throwable e) {
		getDefault().getLog().log(new Status(severity, PLUGIN_ID, message, e));
	}

	@Override
	protected void initializeImageRegistry(ImageRegistry registry) {
		addImage(registry, ENABLED_IMAGE_ID);
		addImage(registry, DISABLED_IMAGE_ID);
        addImage(registry, PREFS_IMAGE_ID);
	}

    private void addImage(ImageRegistry registry, String imageId) {
		registry.put(imageId, ImageDescriptor.createFromURL(FileLocator.find(Platform.getBundle(PLUGIN_ID), new Path(imageId), null)));
	}
}
